package com.example.testservice1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestActivity extends Activity {

	private Button btnStartService1;
	private Button btnStopService1;
	private Button btnTestDownload;
	private Button btnTestExternalStorage;
	
	private final 
	OnClickListener btnStartService1_Click= new OnClickListener() {
		@Override
		public void onClick(View v) {
			
		}
	};
	
	private final 
    OnClickListener btnStopService1_Click= new OnClickListener() {
        @Override
        public void onClick(View v) {
            
        }
    };
    
    private final 
    OnClickListener btnTestDownload_Click= new OnClickListener() {
        @Override
        public void onClick(View v) {
            String urlPath = "http://www.vogella.com/index.html";
            String fileName = "test.html";
            File output = new File(Environment.getExternalStorageDirectory(), fileName);
            if (output.exists()) {
                output.delete();
            }
            
            InputStream stream = null;
            //FileOutputStream fos = null;
            try {

                URL url = new URL(urlPath);
                stream = url.openConnection().getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                //fos = new FileOutputStream(output.getPath());
                int next = -1;
                while ((next = reader.read()) != -1) {
                    //fos.write(next);
                }
                // successfully finished
                //result = Activity.RESULT_OK;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                
            }
        }
    };
    
    private final 
    OnClickListener btnTestExternalStorage_Click= new OnClickListener() {
        /* Checks if external storage is available for read and write */
        private boolean isExternalStorageWritable() {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                return true;
            }
            return false;
        }

        /* Checks if external storage is available to at least read */
        private boolean isExternalStorageReadable() {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                return true;
            }
            return false;
        }
        
        @Override
        public void onClick(View v) {
            String msg= (
                "isExternalStorageWritable: " + isExternalStorageWritable() + "\n" + 
                "isExternalStorageReadable: " + isExternalStorageReadable() + "\n"
            );
            Toast.makeText(TestActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    };
	
	private void initizationComponent() {
		btnStartService1= (Button) findViewById(R.id.btnStartService1);
		btnStartService1.setOnClickListener(btnStartService1_Click);
		
		btnStopService1= (Button) findViewById(R.id.btnStopService1);
		btnStopService1.setOnClickListener(btnStopService1_Click);
		
		btnTestDownload= (Button) findViewById(R.id.btnTestDownload);
        btnTestDownload.setOnClickListener(btnTestDownload_Click);
        
        btnTestExternalStorage= (Button) findViewById(R.id.btnTestExternalStorage);
        btnTestExternalStorage.setOnClickListener(btnTestExternalStorage_Click);
        
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		initizationComponent();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
